from appserver import db_store


"""
分区与设施的中间表
"""

class DistrictWithFacility(db_store.Model):
    __tablename__ = "district_with_facility"
    __main__ = "main"

    # autoincrement=True,
    # id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键')
    facility_id = db_store.Column(db_store.Integer, db_store.ForeignKey('facility.id'), primary_key=True, nullable=False, comment=u"设施id")
    district_id = db_store.Column(db_store.Integer, db_store.ForeignKey('district.id'), primary_key=True, nullable=False, comment=u"分区id")

    def __repr__(self):
        return "<DistrictWithFacility %s>" % self.id

    def get_max_id(self):
        val = db_store.session.query(func.max(DistrictWithFacility.id)).scalar()
        if not val:
            return 0
        else:
            return val

    def save(self):
        try:
            db_store.session.add(self)
            db_store.session.commit()
            return True
        except Exception as e:
            db_store.session.rollback()
            print(e)
            return False

    def delete(self):
        try:
            db_store.session.delete(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print(e)
            db_store.session.rollback()
            return False
